package view
{
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * A simple frame based animated extension of Sprite.
	 */
	public class AnimatedSprite extends Sprite
	{
		/**The animation frames*/
		protected var frames:Vector.<Shape>;
		/**The currently visible frame*/
		protected var current_frame:int;
		
		/**
		 * Create a new AnimatedSprite with n frames.
		 * @param	n	Number of frames
		 */
		public function AnimatedSprite(n:int)
		{
			frames = new Vector.<Shape>();
			for (var i:int = 0; i < n; i++)
			{
				frames.push(new Shape());
				frames[i].visible = false;
				addChild(frames[i]);
			}
			frames[0].visible = true;
		}
		
		/**
		 * Set the currently displayed frame.
		 * @param	n
		 */
		public function setFrame(n:int):void
		{
			if (n > frames.length)
			{
				throw new ArgumentError("frame index out of bounds");
			}
			frames[current_frame].visible = false;
			current_frame = n;
			frames[current_frame].visible = true;
		}
		
		/**
		 * Advance to the next frame.
		 */
		public function nextFrame():void
		{
			setFrame((current_frame + 1) % frames.length);
		}
	
	}

}